<?php
/**
 * 
 */
namespace app\manage\controller\agenttype;

use app\manage\controller\Base;

class Handle extends Base
{
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    public function add(){
        $params = $this->params;
        $type_name = $params['type_name'];

        $data = [
            'type_name' => $type_name,
        ];

        $model = new \app\common\model\AgentType;
        if($model->where(['type_name'=>$type_name])->find()){
            $this->error('代理类型名称已经存在');
        }else{
            $model->save($data);
            $CACHE = \app\common\cache\CacheData::getInstance('AgentType');
            $CACHE -> updateCache();
            $this->success('添加成功');
        }

    }

    public function edit(){
        $params = $this->params;
        $id = $params['id'];
        $type_name = $params['type_name'];

        $model = new \app\common\model\AgentType;
        if($model->where(['id'=>['<>',$id], 'type_name'=>$type_name])->find()){
            $this->error('修改失败，该代理类型名称已经存在');
        }else{
            $model->where(['id'=>$id])->update(['type_name'=>$type_name]);
            $CACHE = \app\common\cache\CacheData::getInstance('AgentType');
            $CACHE -> updateCache();
            $this->success('修改成功');
        }

    }

    public function del(){
        $params = $this->params;
        $id = $params['id'];

        $this->error('暂时停止删除功能');

        $model = new \app\common\model\AgentType;
        if($model->where(['id'=>$id])->delete()){
            $CACHE = \app\common\cache\CacheData::getInstance('AgentType');
            $CACHE -> updateCache();
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }
    
}